package com.sx.pipeline.ui.group.option

import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.CollectionUtils
import com.blankj.utilcode.util.ToastUtils
import com.lxj.xpopup.XPopup
import com.sx.pipeline.R
import com.sx.pipeline.adapter.ItemGroupMemberCheckAdapter
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.arouth.RoutePath.Chat.CHAT_GROUP_OPTION
import com.sx.pipeline.databinding.ActGroupOptionListBinding
import com.sx.pipeline.repos.http.res.ContactUserVo
import com.sx.pipeline.ui.group.GroupManagerVm

@Route(path = CHAT_GROUP_OPTION)
class GroupOptionListAct :
    CYDataBindingBaseActivity<ActGroupOptionListBinding, GroupManagerVm>(R.layout.act_group_option_list) {

    @Autowired
    @JvmField
    var optionType = 1 //2 删除 1添加

    @Autowired
    @JvmField
    var id: String = ""

    lateinit var adapter: ItemGroupMemberCheckAdapter
    override fun initViewModel() {
        super.initViewModel()
    }

    override fun initData() {
        super.initData()
        adapter = ItemGroupMemberCheckAdapter(mViewModel.checkContactUserVo)
        mBinding.title.leftView.setOnClickListener {
            finish()
        }
        mBinding.list.layoutManager = LinearLayoutManager(this)
        mBinding.list.adapter = adapter
        if (optionType == 1) {
            mBinding.title.setTitle("添加成员")
            mBinding.tvOut.setText("添加成员")
            mViewModel.getRongContact(id,"")
        } else {
            mBinding.title.setTitle("删除成员")
            mBinding.tvOut.setText("删除成员")
            mBinding.etKeyword.visibility = View.GONE
            mViewModel.groupListById(id)
        }

        mBinding.etKeyword.addTextChangedListener(object : TextWatcher {
            override fun onTextChanged(
                s: CharSequence,
                start: Int,
                before: Int,
                count: Int
            ) {

            }

            override fun beforeTextChanged(
                s: CharSequence,
                start: Int,
                count: Int,
                after: Int
            ) {

            }

            override fun afterTextChanged(s: Editable) {
                var key = mBinding.etKeyword.text.toString()
                mViewModel.getRongContact(id,key)
            }
        })
        adapter.setOnItemClickListener { adapter, view, position ->
            var bean = adapter.getItem(position) as ContactUserVo
            if (mViewModel.checkContactUserVo.contains(bean)) {
                mViewModel.checkContactUserVo.remove(bean)
            } else {
                if(!bean.hasGroup){
                    mViewModel.checkContactUserVo.add(bean)
                }
            }
            changeCheckStatus()
            adapter.notifyDataSetChanged()
        }
        mBinding.viewCheck.setOnClickListener {
            if (!isCheckAll()) {
                mViewModel.checkContactUserVo.clear()
                var listBean=adapter.data as MutableList<ContactUserVo>
                listBean.filter { !it.hasGroup }?.let {
                    mViewModel.checkContactUserVo.addAll(adapter.data)
                }
            } else {
                mViewModel.checkContactUserVo.clear()
            }
            changeCheckStatus()
            adapter.notifyDataSetChanged()
        }
        mBinding.tvOut.setOnClickListener {
            if (CollectionUtils.isEmpty(mViewModel.checkContactUserVo)) {
                ToastUtils.showShort("请选择成员")
            } else {
                XPopup.Builder(this)
                    .asConfirm(
                        "提示",
                        if (optionType == 1) "是否要添加选中的成员？" else "是否要移除选中的成员"
                    ) {
                        if (optionType == 1) {
                            //添加成员
                            mViewModel.addGroup(id)
                        } else {
                            //删除成员
                            mViewModel.removeGroup(id)
                        }
                    }.show()

            }
        }
    }

    fun isCheckAll(): Boolean {
        var size=mViewModel.contactListEvent.value?.filter { !it.hasGroup }?.let {
            it.size
        }
        var checkSize = mViewModel.checkContactUserVo.size
        return size == checkSize
    }

    fun changeCheckStatus() {
        mBinding.viewCheck.isChecked = isCheckAll()
        var checkSize = mViewModel.checkContactUserVo.size
        if (optionType == 1) {
            mBinding.tvOut.setText("添加成员(${checkSize})")
        } else {
            mBinding.tvOut.setText("删除成员(${checkSize})")
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        mViewModel.contactListEvent.observe(this) {
            var selfId = mViewModel.mModel.getUserData()?.userUnitId
            it?.toMutableList()
                ?.let {
                    adapter.setNewInstance(it as MutableList<ContactUserVo>)
                }
            mViewModel.checkContactUserVo.clear()

        }
    }
}